home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / lib / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.4 KB  |  160 lines

  1. /*
  2.  * (c) Copyright 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <GL/gl.h>
  38. #include <GL/glu.h>
  39.  
  40. #define GRIDPOINTS    41
  41.  
  42. static GLboolean    __grid_initialized = GL_FALSE;
  43.  
  44. static GLfloat     grid[GRIDPOINTS][GRIDPOINTS][3] = { 0 };
  45.  
  46. GLvoid
  47. Checkerboard( GLvoid )
  48. {
  49.     GLfloat     x, y;
  50.     GLboolean   useRed = GL_TRUE;
  51.  
  52.     static GLfloat    redColor[] = { 1.0, 0.0, 0.0 };
  53.     static GLfloat    blackColor[] = { 0.0, 0.0, 0.0 };
  54.  
  55.     glPushAttrib( GL_ENABLE_BIT );
  56.     glDisable( GL_LIGHTING );
  57.     for ( x = -1.0; x < 1.0; x += 0.1 ) {
  58.         useRed = !useRed;
  59.         for ( y = -1.0; y < 1.0; y += 0.1 ) {
  60.             if ( useRed )
  61.                 glColor3fv( redColor );
  62.             else
  63.                 glColor3fv( blackColor );
  64.             glRectf( x, y, x + 0.1, y + 0.1 );
  65.             useRed = !useRed;
  66.         }
  67.     }
  68.     glPopAttrib( );
  69. }
  70.  
  71. GLvoid
  72. _InitGrid( GLvoid )
  73. {
  74.     int i, j;
  75.     GLfloat     x, y, spacing;
  76.  
  77.     /* Create a flat grid composed of points
  78.      * with the z value = 0
  79.      */
  80.     
  81.     /* spacing = distance between adjacent grid points in x and y */
  82.     spacing = 2.0 / (float)(GRIDPOINTS - 1);
  83.  
  84.     /* x and z range from -1.0 to 1.0 by spacing interval */
  85.     for ( x = -1.0, i = 0; i < GRIDPOINTS; x += spacing, i++ ) {
  86.         for ( y = -1.0, j = 0; j < GRIDPOINTS; y += spacing, j++ ) {
  87.             grid[i][j][0] = x;
  88.             grid[i][j][1] = y;
  89.             grid[i][j][2] = 0.0;
  90.         }
  91.     }
  92.  
  93.     __grid_initialized = GL_TRUE;
  94. }
  95.  
  96. GLvoid
  97. WireGrid( GLvoid )
  98. {
  99.     register int i, j;
  100.  
  101.     if ( !__grid_initialized )
  102.         _InitGrid(); 
  103.  
  104.     glNormal3f( 0.0, 0.0, 1.0 );
  105.  
  106.     /* draw vertical lines connecting grid points */
  107.     for ( i = 0; i < (GRIDPOINTS); i++ ) {
  108.         glBegin( GL_LINE_STRIP );
  109.         for ( j = 0; j < (GRIDPOINTS); j++ ) {
  110.             glVertex3fv( &grid[i][j][0] );
  111.         }
  112.         glEnd();
  113.     }
  114.     /* draw horizontal lines connecting grid points */
  115.     for ( j = 0; j < (GRIDPOINTS); j++ ) {
  116.         glBegin( GL_LINE_STRIP );
  117.         for ( i = 0; i < (GRIDPOINTS); i++ ) {
  118.             glVertex3fv( &grid[i][j][0] );
  119.         }
  120.         glEnd();
  121.     }
  122. }
  123.  
  124.  
  125. GLvoid
  126. SolidGrid( GLvoid )
  127. {
  128.     register int i, j;
  129.  
  130.     if ( !__grid_initialized )
  131.         _InitGrid(); 
  132.  
  133.     glNormal3f( 0.0, 0.0, 1.0 );
  134.     
  135.     /* Draw quad strips connecting grid points. 
  136.      * Grid is drawn column by column, 
  137.      * where each column is one quad strip.
  138.      * Every pair of vertices after first two vertices
  139.      * adds a new quad to the strip.
  140.      */
  141.     for ( i = 0; i < (GRIDPOINTS - 1); i++ ) {
  142.         glBegin( GL_QUAD_STRIP );
  143.         for ( j = 0; j < (GRIDPOINTS); j++ ) {
  144.             glVertex3fv( &grid[i][j][0] );
  145.             glVertex3fv( &grid[i + 1][j][0] );
  146.         }
  147.         glEnd();
  148.     }
  149. }
  150.  
  151. /**************************************************************************
  152.  *   Quit() - convenience function for exiting programs
  153.  **************************************************************************/
  154.  
  155. GLvoid
  156. Quit( GLvoid )
  157. {
  158.     exit( 0 );
  159. }
  160.